package com.hwpt.enumeration;

/**
 * 统一状态码枚举
 * 遵循类似HTTP状态码的分段原则：
 * 2xx - 成功
 * 4xx - 客户端错误
 * 5xx - 服务端错误
 * 6xx - 业务自定义错误（可选）
 */
public enum ResultCode {
    // --- 2xx Success ---
    SUCCESS(200, "操作成功"),
    CREATED(201, "创建成功"),
    ACCEPTED(202, "请求已接受"),

    // --- 4xx Client Errors ---
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未授权"),
    FORBIDDEN(403, "禁止访问"),
    NOT_FOUND(404, "资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不允许"),

    // --- 5xx Server Errors ---
    INTERNAL_SERVER_ERROR(500, "系统内部错误"),
    SERVICE_UNAVAILABLE(503, "服务暂不可用"),

    // --- 6xx Business Errors (示例) ---
    BUSINESS_ERROR(600, "业务异常"),
    VALIDATION_ERROR(601, "参数验证失败"),
    DUPLICATE_ENTRY(602, "重复录入");

    public final int code;
    public final String message;

    ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 根据code获取枚举
     */
    public static ResultCode getByCode(int code) {
        for (ResultCode status : values()) {
            if (status.code == code) {
                return status;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }
}